/****************************************************************************
 * 2025.8 柯南
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurvivor
{
	public partial class CoinUpgradePanel : UIElement,IController
	{
		private void Awake()
		{
			foreach (var coinUpgradeItem in this.GetSystem<CoinUpgradeSystem>().Items)
			{
				CoinUpgradItemTemplate.InstantiateWithParent(CoinUpgradItemRoot)
				.Self(self=>
				{
					var itemCache = coinUpgradeItem;
					if ("金币的掉落概率升级" == coinUpgradeItem.Description)
					{
						self.GetComponentInChildren<Text>().text = coinUpgradeItem.Description+"(lv."+Global.Coinlv+")" + "-("+coinUpgradeItem.Price + ")金币";
						self.onClick.AddListener(() =>
						{
							if (Global.Coin.Value >= coinUpgradeItem.Price)
							{
								Global.Coin.Value -= coinUpgradeItem.Price;
								coinUpgradeItem.Upgrade();
								self.GetComponentInChildren<Text>().text = coinUpgradeItem.Description+"(lv."+Global.Coinlv+")" + "-("+coinUpgradeItem.Price + ")金币";
								AudioKit.PlaySound("levelup");
							}

						});
					}
					else
					{
						self.GetComponentInChildren<Text>().text = coinUpgradeItem.Description + "-("+coinUpgradeItem.Price + ")金币";
						self.onClick.AddListener(() =>
						{
							if (Global.Coin.Value >= coinUpgradeItem.Price)
							{
								Global.Coin.Value -= coinUpgradeItem.Price;
								coinUpgradeItem.Upgrade();
								AudioKit.PlaySound("levelup");
							}

						});
					}
						
					var _selfCache=self;
					Global.Coin.RegisterWithInitValue(coin =>
					{
						if (CoinText)
						{
							CoinText.text = "金币:" + coin;
							if (coin >= itemCache.Price)
							{
								_selfCache.Show();
							}
							else
							{
								_selfCache.Hide();
							}
						}	
					}).UnRegisterWhenGameObjectDestroyed(self);//忘写了,就会泄露=>不会遗忘
				}).Show();
			}	
			
			BtnClose.onClick.AddListener(()=>
			{
				this.Hide();		
			});
		}

		protected override void OnBeforeDestroy()
		{
		}

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;//初始化（确保系统注册）。
        }
    }
}